import { useLazyQuery, useMutation, useQuery } from "@apollo/client";
import {
  commitCrouse_service,
  getCrouseInfo_service,
  getCrouses_service,
  getCrouses_service_sample
} from "../../graphql/crouse";
import { CourseType } from "../../pages/course/config";
import { message } from "antd";

export const useFetch_getCourses_service = (
  pageNum: number,
  pageSize: number
) => {
  const { loading, refetch, data } = useQuery(getCrouses_service, {
    skip: true,
    variables: { page: { pageNum, pageSize } }
  });
  const floatData = (data) => {
    return data?.getCourses.data.map((item, index) => ({
      ...item,
      key: index
    }));
  };
  data?.getCourses.data;
  // 第一个参数 params 查询表单和 params 参数的结合
  // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
  const handelRefetch = async (params: {
    pageSize?: number;
    current?: number;
    name?: string;
  }) => {
    // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
    // 如果需要转化参数可以在这里进行修改
    const { data: msg, error } = await refetch({
      name: params.name,
      page: { pageNum: params?.current, pageSize: params?.pageSize }
    });
    return {
      data: floatData(msg),
      // success 请返回 true，
      // 不然 table 会停止解析数据，即使有数据
      success: !error,
      // 不传会使用 data 的长度，如果是分页一定要传
      total: msg?.getCourses.page.total ?? 0
    };
  };
  return {
    loading,
    refetch: handelRefetch,
    page: data?.getCourses.page,
    data: floatData(data)
  };
};

export const useFetch_getCourses_sample = () => {
  const [get, { data, loading }] = useLazyQuery(getCrouses_service, {
    fetchPolicy: "network-only"
  });

  const getCourses = (name: string) => {
    void get({
      variables: {
        name,
        page: {
          pageNum: 1,
          pageSize: 10
        }
      }
    });
  };
  return {
    loading,
    data: data?.getCourses.data as CourseType[],
    getCourses
  };
};

export const useFetch_commitCrouse_service = () => {
  const [edit, { loading }] = useMutation(commitCrouse_service);

  const handleEdit = async (
    id: string,
    params: Partial<CourseType>,
    callback?: (isReload: boolean) => void
  ) => {
    const res = await edit({
      variables: {
        id,
        params
      }
    });
    if (res.data) {
      void message.success(res.data?.commitCourseInfo.message);
      // 刷新课程列表
      callback?.(true);
      return;
    }
    void message.error(res.data?.commitCourseInfo.message);
  };

  return [handleEdit, loading];
};

export const useFetch_getCrouseInfo_service = () => {
  const [get, { loading }] = useLazyQuery(getCrouseInfo_service, {
    fetchPolicy: "network-only"
  });

  const getCourseInfo = async (id: string) => {
    const data = await get({
      variables: {
        id
      }
    });
    return data.data.getCourseInfo.data;
  };

  return [getCourseInfo, loading];
};

export interface sampleDataType {
  name: string;
  id: string;
}

export const useFetchgetCrouses_sample = () => {
  const { data } = useQuery(getCrouses_service_sample, {
    variables: {
      page: {
        pageNum: 1,
        pageSize: 0
      }
    }
  }) as { data: { getCourses: { data: sampleDataType[] } } };
  return data?.getCourses.data;
};
